package com.wskj.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;


/**
 * @author 胡洋
 * @describe 根据表的名称 生成 dao的接口以及到的实现类 其中dao的接口中包含基本的增加删除 修改 查询的方法
 * @Date 2018年3月20日上午9:44:19
 * @version 1.0
 */
public class GenTableDao {

	private String entityPath = "com.wskj.library.pojo";// 指定实体生成所在包的路径
	private String daoPath = "com.wskj.library.dao";//指定dao成所在包的路径
	private String authorName = "T148";// 作者名字
	private String tablename = "fineType";//表名
	private String keyName="tid";//主键字段
	/**
	 * 构造函数
	 */
	public GenTableDao() {
		// 获取 java接口中的所有的文本的内容
		String content = createContent();
		//实现类中的 字符内容
		String daoImplContent = createImplContent();
		try {
			File directory = new File("");
			String daoPath = directory.getAbsolutePath() + "/src/" + this.daoPath.replace(".", "/") 
			+ "/"+ initcap(tablename) + "Dao.java";
			System.out.println(daoPath);
			String daoImplPath = directory.getAbsolutePath() + "/src/" + this.daoPath.replace(".", "/") + 
					"/impl/"+ initcap(tablename) + "DaoImpl.java";
			// 自己创建包 目录...
			String packagePath = directory.getAbsolutePath() + "/src/" + this.daoPath.replace(".", "/") + "/impl";
			File pack = new File(packagePath);
			if (!pack.exists()) {
				pack.mkdirs();
			}
			FileWriter fw = new FileWriter(daoPath);
			PrintWriter pw = new PrintWriter(fw);
			pw.println(content);
			// dao的实现类的方法
			FileWriter fwImpl = new FileWriter(daoImplPath);
			PrintWriter pwImpl = new PrintWriter(fwImpl);
			pwImpl.println(daoImplContent);
			pw.flush();
			pwImpl.flush();
			pw.close();
			pwImpl.close();
			fw.close();
			fwImpl.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成dao接口的代码  
	 * @return 
	 */
	private String createContent() {
		StringBuffer sb = new StringBuffer();
		sb.append("package " + this.daoPath + ";\r\n");
		sb.append("\r\n");
		sb.append("import " + entityPath + ".*;\r\n\r\n");
		sb.append("import java.util.List;\r\n\r\n");
		// 注释部分
		sb.append("/**\r\n");
		sb.append(" * " + tablename + " dao接口类\r\n");
		sb.append(" * " + LocalDate.now().toString() + " " + this.authorName + "\r\n");
		sb.append(" */ \r\n");
		// 实体部分
		sb.append("public interface " + initcap(tablename) + "Dao{\r\n\r\n");
		createInsertMethod(sb, 1); // 新增的方法
		createDeleteMethod(sb, 1); // 删除的方法
		createUpdateMethod(sb, 1); // 修改的方法
		createGetOneMethod(sb, 1); // 获取单个实体的方法
		createGetListMethod(sb, 1); // 获取实体集合的方法
		sb.append("}\r\n");
		return sb.toString();
	}

	/**
	 * @return 生成 dao实现类的文本的内容 ...
	 */
	private String createImplContent() {
		StringBuffer sb = new StringBuffer();
		sb.append("package " + this.daoPath + ".impl;\r\n");
		sb.append("\r\n");
		sb.append("import " + entityPath + ".*;\r\n\r\n");
		sb.append("import " + this.daoPath + ".*;\r\n\r\n");
		sb.append("import java.util.List;\r\n\r\n");
		// 注释部分
		sb.append("/**\r\n");
		sb.append(" * " + tablename + " dao接口类实现类\r\n");
		sb.append(" * " + LocalDate.now().toString() + " " + this.authorName + "\r\n");
		sb.append(" */ \r\n");
		// 实体部分
		sb.append("public class " + initcap(tablename) + "DaoImpl extends BaseDao implements " 
		    + initcap(tablename)+ "Dao{\r\n\r\n");
		createInsertMethod(sb, 2);
		createDeleteMethod(sb, 2);
		createUpdateMethod(sb, 2);
		createGetOneMethod(sb, 2);
		createGetListMethod(sb, 2);
		sb.append("}\r\n");
		return sb.toString();
	}

	/**
	 * 获取实体列表的方法
	 * 
	 * @param sb
	 * @param choise  1是接口的方法 2是实现类的方法
	 */
	private void createGetListMethod(StringBuffer sb, int choise) {
		switch (choise) {
		case 1:			
			sb.append("\t /**\r\n");
			sb.append("\t  * 查询所有的实体的集合\r\n");
			sb.append("\t  * @return 返回查询的结果集\r\n");
			sb.append("\t  */ \r\n");			
			sb.append("\t List<" + initcap(tablename) + ">  getAll" + initcap(tablename) + "s();\r\n");
			break;
		case 2:
			sb.append("\t @Override\r\n");
			sb.append("\t public  List<" + initcap(tablename) + ">  getAll" + initcap(tablename) + "s(){\r\n");
			sb.append("\t\t String sql=\"select * from " + tablename + "\";\r\n");
			sb.append("\t\t Object[] params= null;\r\n");
			sb.append("\t\t return super.queryAll(sql, params, " + initcap(tablename) + ".class);\r\n");
			sb.append("\t }\r\n");
			break;
		}
	}

	/**
	 * 生成获取单个实体的方法
	 * 
	 * @param sb
	 * @param choise  1是接口的方法 2是实现类的方法
	 */
	private void createGetOneMethod(StringBuffer sb, int choise) {
		switch (choise) {
		case 1:
			// 注释部分
			sb.append("\t /**\r\n");
			sb.append("\t  * 根据主键获取实体\r\n");
			sb.append("\t  * @param id 主键\r\n");
			sb.append("\t  * @return 该主键对应的实体\r\n");
			sb.append("\t  */ \r\n");	
			sb.append("\t " + initcap(tablename) + " get" + initcap(tablename) + "ById(int id);\r\n");
			break;
		case 2:
			sb.append("\t @Override\r\n");
			sb.append("\t public " + initcap(tablename) + " get" + initcap(tablename) + "ById(int id){\r\n");
			sb.append("\t\t String sql=\"select * from " + tablename + " where "+keyName+"=? \";\r\n");
			sb.append("\t\t Object[] params= {id};\r\n");
			sb.append("\t\t return super.queryOne(sql, params," + initcap(tablename) + ".class);\r\n");
			sb.append("\t }\r\n");
			break;
		}
	}

	/**
	 * 生成修改的方法
	 * 
	 * @param sb
	 */
	private void createUpdateMethod(StringBuffer sb, int choise) {		
		switch (choise) {
		case 1:
			sb.append("\t /**\r\n");
			sb.append("\t  * 修改更新实体\r\n");
			sb.append("\t  * @param "+initcap(tablename)+" 实体对象\r\n");
			sb.append("\t  * @return 影响的行数 \r\n");
			sb.append("\t  */ \r\n");
			sb.append("\t int update" + initcap(tablename) + "(" + initcap(tablename) + " " +lowerFirst(tablename) + ");\r\n");
			break;
		case 2:
			sb.append("\t @Override\r\n");
			sb.append("\t public int update" + initcap(tablename) + "(" + initcap(tablename) + " " + lowerFirst(tablename) + "){\r\n");
			sb.append("\t\t String sql=\"update "+tablename+" set \";\r\n");
			sb.append("\t\t Object[] params= {};\r\n");
			sb.append("\t\t return super.executeUpdate(sql, params);\r\n");
			sb.append("\t }\r\n");
			break;
		}
	}

	/**
	 * 生成删除的方法 
	 * @param sb
	 */
	private void createDeleteMethod(StringBuffer sb, int choise) {		
		switch (choise) {
		case 1:	
			sb.append("\t /**\r\n");
			sb.append("\t  * 根据主键删除实体\r\n");
			sb.append("\t  * @param id 主键\r\n");
			sb.append("\t  * @return 影响的行数\r\n");
			sb.append("\t  */ \r\n");
			sb.append("\t int deleteById(int id);\r\n");
			break;
		case 2:
			sb.append("\t @Override\r\n");
			sb.append("\t public int deleteById(int id){\r\n");
			sb.append("\t\t String sql=\"delete from "+tablename+"  where "+keyName+"=? \";\r\n");
			sb.append("\t\t Object[] params= {id};\r\n");
			sb.append("\t\t return super.executeUpdate(sql, params);\r\n");
			sb.append("\t }\r\n");
			break;
		}
	}

	/**
	 * 生成新增的方法
	 * 
	 * @param sb
	 */
	private void createInsertMethod(StringBuffer sb, int choise) {		
		switch (choise) {
		case 1:
			sb.append("\t /**\r\n");
			sb.append("\t  * 新增实体\r\n");
			sb.append("\t  * @param "+initcap(tablename)+" 实体对象\r\n");
			sb.append("\t  * @return 影响的行数\r\n");
			sb.append("\t  */ \r\n");
			sb.append("\t int add" + initcap(tablename) + "(" + initcap(tablename) + "  " + lowerFirst(tablename) + ");\r\n");
			break;
		case 2:
			sb.append("\t @Override\r\n");
			sb.append("\t public int add" + initcap(tablename) + "(" + initcap(tablename) + "  " + lowerFirst(tablename) + "){\r\n");;
			sb.append("\t\t String sql=\"insert into "+tablename+"  values  ()\";\r\n");
			sb.append("\t\t Object[] params= {};\r\n");
			sb.append("\t\t return super.executeUpdate(sql, params);\r\n");
			sb.append("\t }\r\n");
			break;
		}
	}

	/**
	 * 功能：将输入字符串的首字母改成大写 
	 * @param str
	 * @return
	 */
	private String initcap(String str) {
		char[] ch = str.toCharArray();
		if (ch[0] >= 'a' && ch[0] <= 'z') {
			ch[0] = (char) (ch[0] - 32);
		}
		return new String(ch);
	}
	
	/**
	 * 将字符串的首字母改为小写
	 * @param str
	 * @return
	 */
	public String  lowerFirst(String str) {
		char[] ch = str.toCharArray();
		if (ch[0] >= 'A' && ch[0] <= 'Z') {
			ch[0] = (char) (ch[0]+32);
		}
		return new String(ch);
	}

	/**
	 * 出口 TODO
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new GenTableDao();
	}

}
